package com.huluobo.a2201mvvm_imdemo.contact;


import android.util.Log;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;


import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2201mvvm_imdemo.R;
import com.huluobo.a2201mvvm_imdemo.adapter.ContactAdapter;
import com.huluobo.a2201mvvm_imdemo.databinding.FragmentMyContactBinding;
import com.huluobo.a2201mvvm_imdemo.mvvm.BaseMVVMFragment;
import com.huluobo.a2201mvvm_imdemo.view.SlideBar;
import com.hyphenate.EMContactListener;
import com.hyphenate.chat.EMClient;

import java.util.ArrayList;
import java.util.List;

public class MyContactFragment extends BaseMVVMFragment<FragmentMyContactBinding, ContactViewModel> {

    private List<ContactListItemBean> contactListItemBeans = new ArrayList<>();

    private ContactAdapter contactAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_contact;
    }

    @Override
    protected void initData() {
        viewModel.getContactsSuccess.observe(requireActivity(), contactListItemBeans -> {
            dataBinding.contactSrl.setRefreshing(false);
            this.contactListItemBeans.clear();
            this.contactListItemBeans.addAll(contactListItemBeans);
            contactAdapter = new ContactAdapter(contactListItemBeans);
            dataBinding.myContactRv.setAdapter(contactAdapter);

            contactAdapter.notifyDataSetChanged();
        });
        viewModel.getContactsFailed.observe(requireActivity(), s -> {
            dataBinding.contactSrl.setRefreshing(false);
            Log.i("TagA", "获取联系人失败:" + s);
        });
    }

    @Override
    protected void initView() {
        viewModel.getAllContact();

        dataBinding.myContactRv.setLayoutManager(new LinearLayoutManager(requireContext()));
        contactAdapter = new ContactAdapter(contactListItemBeans);
        dataBinding.myContactRv.setAdapter(contactAdapter);

        dataBinding.contactSrl.setOnRefreshListener(() -> {
            viewModel.getAllContact();
        });

        dataBinding.myContactSlideBar.setOnSectionChangeListener(new SlideBar.OnSectionChangeListener() {
            @Override
            public void onSectionChange(String firstLetter) {
                dataBinding.myContactSection.setVisibility(View.VISIBLE);
                dataBinding.myContactSection.setText(firstLetter);
                moveToLetterPosition(firstLetter);
            }

            @Override
            public void onSlideFinish() {
                dataBinding.myContactSection.setVisibility(View.GONE);
            }
        });

        EMClient.getInstance().contactManager().setContactListener(new EMContactListener() {
            @Override
            public void onContactAdded(String username) {

            }

            @Override
            public void onContactDeleted(String username) {
                //监听到好友被删除,需要重新获取一下好友列表
                viewModel.getAllContact();
            }

            @Override
            public void onContactInvited(String username, String reason) {

            }

            @Override
            public void onFriendRequestAccepted(String username) {

            }

            @Override
            public void onFriendRequestDeclined(String username) {

            }
        });

        dataBinding.myContactSearchBtn.setOnClickListener(v -> {
            String username = dataBinding.myContactEt.getText().toString();
            if (username.isEmpty()) {
                Log.i("TagA", "输入框等于空");
                viewModel.getAllContact();
                return;
            }
            List<ContactListItemBean> newList = new ArrayList<>();
            for (int i = 0; i < contactListItemBeans.size(); i++) {
                if (username.equals(contactListItemBeans.get(i).getUsername())) {
                    ContactListItemBean contactListItemBean = new ContactListItemBean(username, username.charAt(0),
                            true);
                    newList.add(contactListItemBean);
                }
            }

            contactAdapter = new ContactAdapter(newList);
            dataBinding.myContactRv.setAdapter(contactAdapter);
        });
    }

    //根据首字符滚动到对应的位置
    private void moveToLetterPosition(String firstLetter) {
        for (int i = 0; i < contactListItemBeans.size(); i++) {
            //对比首字符是否相等
            if (String.valueOf(contactListItemBeans.get(i).getFirstLetter()).equals(firstLetter)) {
                dataBinding.myContactRv.smoothScrollToPosition(i);//平滑移动到对应的position位置
            }
        }
    }

    @Override
    protected Class<ContactViewModel> getViewModel() {
        return ContactViewModel.class;
    }
}